package net.slteam.base;

import java.io.Serializable;

/**
 * ClassName: SLError
 * Description:
 * date: 6/21/21 12:00 PM
 *
 * @author calm
 * @since JDK 1.8
 */
public class SLError extends Exception implements Serializable, IError {

    private static final long serialVersionUID = 1L;

    Integer code;

    String domain = DOMAIN_SLTEAM;

    String message;

    public SLError() {
    }

    public SLError(String message) {
        this.message = message;
    }

    public SLError(IError error) {
        domain = DOMAIN_SLTEAM;
        code = error.getCode();
        message = error.getMessage();
    }

    /**
     * Constructs a new exception with {@code null} as its detail message.
     * The cause is not initialized, and may subsequently be initialized by a
     * call to {@link #initCause}.
     */
    public SLError(Integer code,
                   String domain,
                   String message) {
        this.code = code;
        this.domain = domain;
        this.message = message;
    }

    public static SLErrorBuilder builder() {
        return new SLErrorBuilder();
    }

    @Override
    public Integer getCode() {
        return this.code;
    }

    public void setCode(final Integer code) {
        this.code = code;
    }

    @Override
    public String getDomain() {
        return this.domain;
    }

    public void setDomain(final String domain) {
        this.domain = domain;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "SLError(code=" + this.getCode() + ", domain=" + this.getDomain() + ", message=" + this.getMessage() + ")";
    }


    public static class SLErrorBuilder {
        private Integer code;
        private String domain = DOMAIN_SLTEAM;
        private String message;

        SLErrorBuilder() {
        }

        public SLErrorBuilder code(final Integer code) {
            this.code = code;
            return this;
        }

        public SLErrorBuilder domain(final String domain) {
            this.domain = domain;
            return this;
        }

        public SLErrorBuilder message(final String message) {
            this.message = message;
            return this;
        }

        public SLError build() {
            return new SLError(this.code, this.domain, this.message);
        }

        @Override
        public String toString() {
            return "SLError.SLErrorBuilder(code=" + this.code + ", domain=" + this.domain + ", message=" + this.message + ")";
        }
    }
}
